//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    partial class HomeTabContent
    {
        public HomeTabContent()
        {
            InitializeComponent();
        }

        void onStyleChanged (object sender, SelectionChangedEventArgs args)
        {
            Selector selector = (Selector) sender;
            SpreadsheetWindow w = WindowServices.GetAncestor<SpreadsheetWindow>(selector);
            if   (w != null && w.InChange)
                return;
            CellStyle style = (CellStyle) selector.SelectedItem;
            if (style == null)
                return;
            Range selection = SpreadsheetElement.GetSelection(sender);
            if (selection == null)
                return;

            selection.Worksheet.Workbook.InPreviewMode = false;
            if (!selection.Worksheet.InSelectionChanged)
                selection.SetStyle(style);
            args.Handled = true;

        }
        void onFontFamilyChanged(object sender, SelectionChangedEventArgs args)
        {
          //\\  Debug.WriteLine("onFontfamilychanged");
            Range selection = SpreadsheetElement.GetSelection(sender);
            if (selection == null)
                return;
            if (selection.Worksheet.InSelectionChanged)
                return;
            args.Handled = true;
            var cb = (ComboBox)sender;
            FontFamily f;
            {
                f = (FontFamily)cb.SelectedItem;
            }
            if (f == null)
                return;
            selection.Worksheet.Workbook.InPreviewMode = false;
            //\\GridCommandHelper.SetLastExecutedCommand(sender, new ExecutedCommand(SpreadsheetEditingCommands.SetFontFamily, f));
            ((ICommand)SpreadsheetEditingCommands.SetFontFamily).Execute(f);
            
            //\\selection.SetCellValue(CellProperty.FontFamily, f);
        }

        private void onFontSizeChanged(object sender, SelectionChangedEventArgs args)
        {
            Range selection = SpreadsheetElement.GetSelection(sender);
            if (selection == null)
                return;
            if (selection.Worksheet.InSelectionChanged)
                return;
            object v = ((ComboBox)sender).SelectedItem;
            if (v == null)
                return;
            var f = (double)v;
            selection.Worksheet.Workbook.InPreviewMode = false;
            ((ICommand)SpreadsheetEditingCommands.SetFontSize).Execute(f);
            //GridCommandHelper.SetLastExecutedCommand(sender, new ExecutedCommand(SpreadsheetEditingCommands.SetFontSize, f));
            //selection.SetCellValue(CellProperty.FontSize, f);
        }

        private void onFontFamilyPreviewChanged(object sender, RoutedEventArgs args)
        {
            applyPreview(sender, args, (Range range,FontFamily data) => range.SetCellValue(CellProperty.FontFamily, data ));
        }

        void onBackgroundPreviewChanged(object sender, RoutedEventArgs args)
        {
            applyPreview(sender, args, (Range range, SpreadsheetColor data) => range.Background = PatternFill.NewForegroundSolidFill(data));
        }
        void onForegroundPreviewChanged(object sender, RoutedEventArgs args)
        {
            applyPreview(sender, args, (Range range, SpreadsheetColor data) => range.Foreground = data);
        }
       
        void onFontSizePreviewChanged(object sender, RoutedEventArgs args)
        {
            applyPreview(sender, args, (Range range,double data) => range.FontSize=data);
        }
    
        void onConditionalFormattingChanged (object sender, RoutedEventArgs args)
        {
            applyPreview(sender,args,
                             (Range range, ConditionalFormattingRule formattingRule) =>
                             range.Worksheet.ApplyFormattingRule(formattingRule.Clone(), range));
            return;
        }

        private void onCellStylePreviewChanged(object sender, RoutedEventArgs args)
        {
            applyPreview(sender, args, (Range range,CellStyle data) => range.SetStyle(data));
        }

        private void onTableStylePreviewChanged(object sender, RoutedEventArgs args)
        {
            applyPreview(sender, args, delegate(Range range, TableStyle style)
                                               {
                                                   foreach (Table table in range.Worksheet.Tables)
                                                   {
                                                       if (!Range.Intersects(table.Range, range))
                                                           continue;
                                                       table.Style = style;
                                                   }
                                                   
                                               });
            return;
        }

        private delegate void RangeAction<T>(Range range, T data);

        static void applyPreview<T>(object sender, RoutedEventArgs args, RangeAction<T> action)
        {
            Range selection = SpreadsheetElement.GetSelection(sender);
            if (selection == null)
                return;
            object preview = HighlightElement.GetHighlightedItem((DependencyObject) sender);
            {
                if (preview == null)
                {
                    selection.Worksheet.Workbook.InPreviewMode = false;
                    return;
                }
                selection.Worksheet.Workbook.InPreviewMode = true;
                action(selection, (T)preview);
            }
            args.Handled = true;
        }

        void onFormatChanged(object sender, SelectionChangedEventArgs args)
        {
            var cb = (ComboBox) args.Source;
            var b = new Binding("ActiveCell.Format.Category") {Mode = BindingMode.OneWay};
            cb.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(
                                                                         () => cb.SetBinding(ComboBox.TextProperty, b)));
            args.Handled = true;
        }


        
        private void onParentMenuItemClick(object sender, RoutedEventArgs e)
        {
            MenuItem parent = (MenuItem) sender;
            MenuItem child = (MenuItem) e.OriginalSource;
            if (parent == child)
                return;
            parent.CommandParameter = child.CommandParameter;

        }
    }
}
